$(document).ready(function()
		{	
   			$("#J_registerEmail").blur(function() {
				var email = $.trim($("#J_registerEmail").val());
				var vatidateMail = checkMail(email);
			}); 
    		
   			var checkEmailReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
   			
   			$("#J_userDetailsNickname").blur(function() {
				var nickname = $.trim($("#J_userDetailsNickname").val());
				var nickNameValitate = chceknickName(nickname);
				if(!nickNameValitate)
				{
					return false;
				}else
				{
					$.ajax({
					    url: "/registercheck?nickName="+nickname+"&date="+(new Date()).getTime(),
					    dataType: 'json',
					    type: "POST",
					    success: function(data)
					    {
					    	if(!data.success){
						  		$("#J_userDetailsNickname_error").text("昵称已被使用!");
						  		return false;	
							 }
					    }
					});		
				}	
			});
   			
   			$("#J_code_query").blur(function() {
   				$("#J_code_query_error").text("");
			});
   			
			$("#J_register").click(function()
			{	
				var email = $.trim($("#J_registerEmail").val());
				var password = $.trim($("#J_enterPass").val());
				var userDetailsNickname = $.trim($("#J_userDetailsNickname").val());
				var postCode = $.trim($("#J_userDetailsPostCode_register").val());
				var cityCode = $.trim($("#J_userDetailsCityCode_register").val());
				var city = $.trim($("#J_userDetailsCity_register").val());
				var provinceCode = $.trim($("#J_userDetailsProvinceCode_register").val());
				var province = $.trim($("#J_userDetailsProvince_register").val());
				var checkCode =  $.trim($("#J_code_query").val());
				
				checkPass();
				
				//验证邮箱
				var vatidateMail = checkMail(email);
				if(!vatidateMail)
				{
					return false;
				}
				
				var nickNameValitate = chceknickName(userDetailsNickname);
				if(!nickNameValitate)
				{
					return false;
				}
				
				var validateCode = checkValidate(checkCode);
				if(!validateCode)
				{
					return false;
				}
				
				//是否选择周意了协议
				if(!$("#J_agree").is(":checked")){
				    jAlert("请阅读并同意KIMOJI注册协议，才能进一步注册！");
				    document.getElementById("J_forget").style.display="none";
				    return false;						
				}
				$.ajax({
				    url: "/registercheck?userAccount="+encodeURIComponent(email)+"&nickName="+encodeURIComponent(userDetailsNickname)+"&validcode="+encodeURIComponent(checkCode)+"&date="+(new Date()).getTime(),
				    dataType: 'json',
				    type: "POST",
				    success: function(data)
				    {
						if(data.errorMessages.state == "unactivated")
						 {
						 	var accountEmail = data.errorMessages.accountEmail;
						 	$("#J_activate").html("该邮箱已注册,<a onClick=sendMail('" +accountEmail+ "',1)  > <font color='red'>激活账号</font></a>");
						 	document.getElementById("J_forget").style.display="none";
						 	document.getElementById("J_activate").style.display="";
							return false;	
						 }
						 if(data.errorMessages.state == "normal")
						 {
						 	document.getElementById("J_forget").style.display="";
						 	document.getElementById("J_activate").style.display="none";
							return false;
						 }
						 
						 if(data.errorMessages.validcode == "codeError")
						 {
							 $("#J_code_query_error").text("验证码错误，请重新输入");
								return false;
						 } 
						 if(!data.success){
					  		$("#J_userDetailsNickname_error").text("昵称已被使用!");
					  		return false;	
						 }
						 $("#userAccountBO").submit();
				    }
				});		
				});
				
			$("#reloadcode").click(function(){
				$("#validcodeImg").attr('src','/validcode.jpg?v='+new Date().getTime());
				return false;
			});
			
			$("#J_enterPass").focus(function() {
				//验证邮箱
				var check = checkMail($.trim($("#J_registerEmail").val()));
				if(check){
					$("#J_registerEmail_error").text("");
					document.getElementById("J_forget").style.display="none";
					document.getElementById("J_forget").style.display="none";
				}
			});
			
			$("#J_registerEmail").focus(function() {
				$("#J_registerEmail_error").text("");
			});
			
			
			$("#J_userDetailsNickname").focus(function() {
				   checkPass();
			});
	
			
	//邮箱校验		
	function  checkMail(email){
		var flag = true;
		if(email ==""||email.length<=0)
		{	
			$("#J_registerEmail_error").text("请输入注册邮箱!");
		 	document.getElementById("J_forget").style.display="none";
		 	flag =  false;
			
		}else if(!email.match(checkEmailReg))
		{
			$("#J_registerEmail_error").text("邮箱格式不正确!");
			document.getElementById("J_forget").style.display="none";
			flag =  false;
		}
		 else if(email.length>50)
		{	
			$("#J_registerEmail_error").text("邮箱长度不能超过50个字符!");
			document.getElementById("J_forget").style.display="none";
			flag =  false;
		}else{
			$.ajax({
			    url: "/registercheck?userAccount="+encodeURIComponent(email)+"&date="+(new Date()).getTime(),
			    dataType: 'json',
			    type: "POST",
			    success: function(data)
			    {
					if(data.errorMessages.state == "unactivated")
					 {
					 	var accountEmail = data.errorMessages.accountEmail;
					 	$("#J_activate").html("该邮箱已注册,<a onClick=sendMail('" +accountEmail+ "',1)  > <font color='red'>激活账号</font></a>");
					 	document.getElementById("J_forget").style.display="none";
					 	document.getElementById("J_activate").style.display="";
					 	flag =  false;	
					 }
					 if(data.errorMessages.state == "normal")
					 {
					 	document.getElementById("J_forget").style.display="";
					 	document.getElementById("J_activate").style.display="none";
					 	flag =  false;
					 }
			    }
			});	
		}
		return flag;
	}
	
	//校验密码
	function checkPass(){
		var vatidateMail = checkMail($.trim($("#J_registerEmail").val()));
		if(vatidateMail)
		{
			$("#J_registerEmail_error").text("");
			document.getElementById("J_forget").style.display="none";
			document.getElementById("J_forget").style.display="none";
		}
		
		var password = $.trim($("#J_enterPass").val());
		if(password.length<6||password.length>16)
		{	
			$("#J_enterPass_error").text("密码长度为6-16位!");
			document.getElementById("J_forget").style.display="none";
			return false;
		}else if(password==""){
			$("#J_enterPass_error").text("请输入密码!");
			return false;
		}else{
			$("#J_enterPass_error").text("");
			return true;
		}
	}
	
	//昵称校验
	function chceknickName(nickName)
	{
		var htmlCode = "[`~!@#$^>&*\<()=|{}':;',\\[\\].\\<>/?~%！@#￥……&*（）——|{}【】‘；：”“'。，、？]";
		
		for ( var i = 0; i < htmlCode.length; i++) 
		{
			if (nickName.indexOf(htmlCode[i])>=0) {
				$("#J_userDetailsNickname_error").text("昵称不能包含非法字符"+htmlCode[i]);
				return false;
			}
		}
		
		if(strlen(nickName)<4||strlen(nickName)>30)
		{	
			$("#J_userDetailsNickname_error").text("昵称长度为4-30个字符或2-15个汉字!");
			document.getElementById("J_forget").style.display="none";
			return false;
		}else{
			$("#J_userDetailsNickname_error").text("");
		}
		return true;
	}
	
	//验证码校验
	function checkValidate(checkCode)
	{
		if(checkCode==""||checkCode.length<=0)
		{	
			$("#J_code_query_error").text("请输入验证码!");
			return false;
		}else
		{
			$("#J_code_query_error").text("");
			return true;
		}	
	}
	
	function strlen(str){
		var len;
		var i;
		len = 0;
		for (i=0;i<str.length;i++){
			if (str.charCodeAt(i)>255){
				len+=2;
			}else{
				 len++;
			}  
		}
		return len;
	}			
});

